from Tkinter import *
from telnetlib import Telnet
import threading
import time

class telThread( threading.Thread ):
	def __init__ ( self, App ):
		self.app = App
		threading.Thread.__init__ ( self )

	def run( self ):
		print "thread running!"
		while(self.app.running):
			self.app.telnet.write("\r\n")
			self.app.data = self.app.telnet.read_some()
		        for i in range(0,6):
		                self.app.audio[i].set(((ord(self.app.data[4+i])-64)*100)/58)
		        for i in range(0,6):
		                self.app.rf[i].set(((ord(self.app.data[10+i])-64)*100)/22)
		        for i in range(0,6):
		                self.app.batt[i].set(((ord(self.app.data[16+i])-73)*100)/17)
		        for i in range(0,6):
		                self.app.chan[i] = self.app.data[22+(i*2):22+(i*2)+2]
		        self.app.status = ord(self.app.data[34]) & ord('\x0f')
		        for i in range(0,6):
		                self.app.mystery[i] = self.app.data[35+i]
			time.sleep(0.1)
		

class venueNetClient:
	def __init__(self, myParent):
		self.running = False
		self.thread = telThread(self)
		self.audio = []
		self.rf = []
		self.batt = []
		self.chan = ['', '', '', '', '', '']
		self.status = '\x00'
		self.mystery = ['', '', '', '', '', '']

		self.telnet = Telnet()
		self.telnet.open("172.18.51.197",8007)
		self.root = myParent
		self.fTools = Frame(self.root)
		self.fTools.grid(row=0,column=0,columnspan=3)
		self.bGo = Button(self.fTools)
		self.bGo.grid(row=0,column=0)
		self.bGo["text"] = "Go!"
		self.bGo["background"] = "green"
		self.bGo.bind("<ButtonRelease-1>", self.barset)
		self.bStop = Button(self.fTools, text="Stop", background="red")
		self.bStop.grid(row=0,column=1)
		self.bStop.bind("<ButtonRelease-1>", self.exit)
		
		#Channel 1
		self.fC1 = Frame(self.root)
		self.fC1.grid(row=1,column=0)
		self.c1T = Label(self.fC1, text="Channel 1").grid(row=0,column=0)
		self.audio.append(Scale(self.fC1, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[0].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC1, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[0].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC1, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[0].grid(row=3,column=0,columnspan=1)

		#Channel 2
		self.fC2 = Frame(self.root)
		self.fC2.grid(row=1,column=1)
		self.c2T = Label(self.fC2, text="Channel 2").grid(row=0,column=0)
		self.audio.append(Scale(self.fC2, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[1].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC2, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[1].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC2, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[1].grid(row=3,column=0,columnspan=1)

		#Channel 3
		self.fC3 = Frame(self.root)
		self.fC3.grid(row=1,column=2)
		self.c3T = Label(self.fC3, text="Channel 3").grid(row=0,column=0)
		self.audio.append(Scale(self.fC3, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[2].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC3, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[2].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC3, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[2].grid(row=3,column=0,columnspan=1)

		#Channel 4
		self.fC4 = Frame(self.root)
		self.fC4.grid(row=2,column=0)
		self.c4T = Label(self.fC4, text="Channel 4").grid(row=0,column=0)
		self.audio.append(Scale(self.fC4, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[3].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC4, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[3].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC4, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[3].grid(row=3,column=0,columnspan=1)

		#Channel 5
		self.fC5 = Frame(self.root)
		self.fC5.grid(row=2,column=1)
		self.c5T = Label(self.fC5, text="Channel 5").grid(row=0,column=0)
		self.audio.append(Scale(self.fC5, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[4].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC5, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[4].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC5, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[4].grid(row=3,column=0,columnspan=1)

		#Channel 6
		self.fC6 = Frame(self.root)
		self.fC6.grid(row=2,column=2)
		self.c6T = Label(self.fC6, text="Channel 6").grid(row=0,column=0)
		self.audio.append(Scale(self.fC6, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.audio[5].grid(row=1,column=0,columnspan=1)
		self.rf.append(Scale(self.fC6, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.rf[5].grid(row=2,column=0,columnspan=1)
		self.batt.append(Scale(self.fC6, from_=0, to=100, orient=HORIZONTAL,length=300))
		self.batt[5].grid(row=3,column=0,columnspan=1)

		#Labels
		#self.fLabel1 = Frame(self.root)
		#self.fLabel1.grid(row=1,column=3)
		#self.fL1A = Label(self.fLabel1, text="Audio").grid(row=1,column=0,rowspan=2)
		#self.fL1R = Label(self.fLabel1, text="RF").grid(row=3,column=0,rowspan=2)
		#self.fL1B = Label(self.fLabel1, text="Batt").grid(row=5,column=0,rowspan=2)

	def exit(self, event):
		self.running = False
		print "Attempting to close thread..."
		self.thread.join()
		print "Thread closed!"
		self.telnet.close()
		self.root.destroy()

	def barset(self, event):
		self.running = True
		self.telnet.write("\r\n")
		self.data = self.telnet.read_some()
	        for i in range(0,6):
	                self.audio[i].set(((ord(self.data[4+i])-64)*100)/58)
	        for i in range(0,6):
	                self.rf[i].set(((ord(self.data[10+i])-64)*100)/22)
	        for i in range(0,6):
	                self.batt[i].set(((ord(self.data[16+i])-73)*100)/17)
	        for i in range(0,6):
	                self.chan[i] = self.data[22+(i*2):22+(i*2)+2]
	        self.status = ord(self.data[34]) & ord('\x0f')
	        for i in range(0,6):
	                self.mystery[i] = self.data[35+i]
		self.thread.start()
#		self.bar.configure(length=200)
#		self.bar.set(random.randint(0,100))

root = Tk()
App = venueNetClient(root)
root.mainloop()
